#include "head.h"
int trantoNum(char *p,int *num) {
     // int length_p = strlen(p);
     // puts(p);
     // p[length_p - 1] = '\0';
     // printf("length_p = %d\n", length_p);
     // memcmp(const void *s1, const void *s2, size_t n);
     if (strcmp(p, "cd") == 0) {
         *num = 0;
     } else if (strcmp(p, "ls") == 0){
         *num = 1;
     } else if (strcmp(p, "puts") == 0) {
         *num = 2;
     } else if (strcmp(p, "gets") == 0) {
         *num = 3;
     } else if (strcmp(p, "remove") == 0) {
         *num = 4;
     } else if (strcmp(p, "pwd") == 0) {
         *num = 5;
     } else if (strcmp(p, "mkdir") == 0) {
         *num = 6;
     } else if (strcmp(p, "rmdir") == 0) {
         *num = 7;
     } else if (strcmp(p, "quit") == 0) {
         *num = 8;
     }
     return 0;
 }
void remove_first_token(char *str) {
    // Find the end position of the first token
    int len = strlen(str);
    int i = 0;
    for (; i < len; i++) {
        if (str[i] == ' ') {
            break;
        }
    }
    // Find the index of the first non-space character after the first token
    for (; i < len; i++) {
        if (str[i] != ' ') {
            break;
        }
    }
    // Shift the remaining characters to the left
    int j = 0;
    for (; i < len - 1; i++) {
        str[j++] = str[i];
    }
    // 这里已经把最后的换行符给移除了
    str[j++] = '\0';
    str[j] = '\0';
}
int removeLeadingSpaces(char* str) {
    int i = 0, j = 0;
    while (str[i] == ' ') {
        i++;
    }
    if (i > 0) {
        while (str[i] != '\0') {
            str[j] = str[i];
            i++;
            j++;
        }
        str[j] = '\0';
        return 1; // 返回 1 表示成功删除空格
    }
    return 0; // 返回 0 表示没有空格可以删除
}
int main(int argc,char* argv[]){
    //./client 192.168.88.129 1234
    if(argc<3){
        error(1,0,"参数缺失\n");
    }
    int sockfd=socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in addr;
    addr.sin_family=AF_INET;
    addr.sin_port=htons(atoi(argv[2]));
    addr.sin_addr.s_addr=inet_addr(argv[1]);
    int ret=connect(sockfd,(struct sockaddr*)&addr,sizeof(addr));
    if(ret==-1){
        error(1,errno,"connect is error\n");
    }
    printf("与服务器链接成功\n");
    
    int ret_num = loginFunc(sockfd);
    if (ret_num == -1) return 0;
    printf("登录成功\n");

    train_t train_command;

    while(1){
        fflush(stdout);
        bzero(&train_command,sizeof(train_command));
        fgets(train_command.data,sizeof(train_command.data),stdin);
        
        removeLeadingSpaces(train_command.data);
        train_command.data[strlen(train_command.data)-1]='\0';
        train_command.data[strlen(train_command.data)]=' ';
        
        char temp[4096];
        strcpy(temp,train_command.data);
        char* token;
        token=strtok(temp," ");
        char optstr[1024];//存储命令
        strcpy(optstr,token);
        remove_first_token(train_command.data);//分离去掉命令
        train_command.length=strlen(train_command.data);

        // 文件名顺便获取
        char filename[256];
        char *filename_start = strrchr(train_command.data, '/');
        if (filename_start != NULL) { // 如果发现'/'，则文件名为'/'之后的内容
            filename_start++; // 跳过'/'，指向文件名的起始位置
        } else {
            filename_start = train_command.data; // 否则整个字符串都是文件名
        }

        strcpy(filename, filename_start); // 复制文件名到新的字符数组中
        filename[strlen(filename)] = '\0';

        int optnum = 999;
        trantoNum(optstr, &optnum);
        
        //向服务器上传操作
        send(sockfd,&optnum,sizeof(int),MSG_NOSIGNAL);
        send(sockfd,&train_command,sizeof(train_command.length)+train_command.length,MSG_NOSIGNAL);

        int iferror;
        switch(optnum){
        case 0:
            // 什么都没做
            cdFunc(sockfd);
           
            //printf("cd %d\n",optnum);
            break;
        case 1:
            lsFunc(sockfd);
            break;
        case 2:
            printf("filename: %s\n",filename);
            putsFunc(sockfd, filename);
            break;
        case 3:
            printf("filename = %s\n", filename);
            // 这里先接收一个整数表示是否是报错信息
            getsFunc(sockfd, filename);
            break;
        case 4:
            removeFunc(sockfd);
            break;
        case 5:
            pwdFunc(sockfd);
            break;
        case 6:
            mkdirFunc(sockfd);
            break;
        case 7:
            rmdirFunc(sockfd);
            break;
        case 8:
           // recv(sockfd, &ifError, sizeof(int), 0);
           // return 0;
        default:
           // recv(sockfd, &ifError, sizeof(int), 0);
            printf("No command found!\n");
        }
        //sleep(2000);
    }
}
